package com.twu.biblioteca.core;

import com.twu.biblioteca.util.Cli;

import java.util.ArrayList;
import java.util.List;

public class Menu {
    private List<Option> options = new ArrayList<>();
    private Cli cli;

    public Menu(Cli cli) {
        this.cli = cli;
    }

    public void addOption(Option option) {
        options.add(option);
    }

    public Option promptOption() {
        int optionIndex = cli.promptInt(display());
        return selectOption(optionIndex);
    }

    private Option selectOption(int optionIndex) {
        try {
            return options.get(optionIndex);
        } catch (Exception e) {
            throw new InvalidMenuOptionException();
        }
    }

    public String display() {
        StringBuilder result = new StringBuilder();
        for (int optionIndex = 0; optionIndex < options.size(); optionIndex++) {
            Option option = selectOption(optionIndex);
            result.append(option.displayOption(optionIndex));
        }
        return result.toString();
    }
}
